package com.zsc.edu.testservice;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class ServiceActivity extends AppCompatActivity {

    final static String TAG = "---ServiceActivity---";

    Button btn_request;
    EditText edit_text_index;
    EditText edit_text_number;
    ImageView image_view;

    RequestService.MyBinder myBinderAct;

    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

            myBinderAct = (RequestService.MyBinder)iBinder;

            // 设置接口，匿名类
            myBinderAct.setOnBitmapLoadListener(new RequestService.OnBitmapLoadListener() {
                @Override
                public void onBitmapLoad(Bitmap bitmap) { // 实现函数
                    // 更新UI
                    image_view.setImageBitmap(bitmap);
                }
            });


            Log.d(TAG, "onServiceConnected " + myBinderAct.getData());
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);

        btn_request = findViewById(R.id.btn_request);
        image_view = findViewById(R.id.image_view);
        edit_text_index = findViewById(R.id.edit_text_index);
        edit_text_number = findViewById(R.id.edit_text_number);

        findViewById(R.id.btn_bind_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ServiceActivity.this,
                        RequestService.class);

                intent.putExtra("data", "Hello Service. I'm ServiceActivity.");

                // 绑定 Service
                bindService(intent,
                        serviceConnection,
                        Service.BIND_AUTO_CREATE); // 绑定服务自动创建
            }
        });

        findViewById(R.id.btn_unbind_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 解绑 Service
                unbindService(serviceConnection);

            }
        });

        btn_request.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ServiceActivity.this,
                        RequestService.class);

                String idx = edit_text_index.getText().toString();
                String num = edit_text_number.getText().toString();
                intent.putExtra("idx", idx);
                intent.putExtra("num", num);

                // 启动 Service
                startService(intent);

            }
        });
    }


}